In [1]:
"""Normalizing"""

import os
import pandas as pd
import matplotlib.pyplot as plt

def plot_selected(df, columns, start_index, end_index):
    """Plot the desired columns over index values in the given range."""
    # TODO: Your code here
    # Note: DO NOT modify anything else!
    plot_data(df.ix[start_index:end_index,columns], title="Selected data")

def symbol_to_path(symbol, base_dir="data/SP500/symbols"):
    """Return CSV file path given ticker symbol."""
    return os.path.join(base_dir, "{}.csv".format(str(symbol)))


def get_data(symbols, dates):
    """Read stock data (adjusted close) for given symbols from CSV files."""
    df = pd.DataFrame(index=dates)
    if 'AAPL' not in symbols:  # add SPY for reference, if absent
        symbols.insert(0, 'AAPL')

    for symbol in symbols:
        df_temp = pd.read_csv(symbol_to_path(symbol), index_col='Date',
                parse_dates=True, usecols=['Date', 'Adj Close'], na_values=['nan'])
        df_temp = df_temp.rename(columns={'Adj Close': symbol})
        df = df.join(df_temp)
        if symbol == 'AAPL':  # drop dates SPY did not trade
            df = df.dropna(subset=["AAPL"])

    return df


def normalize_data(df):
    '''Normalize stock price using the first row of the dataframe'''
    #normalizing_data = df.copy() # copy gibe DtaFrame to match size and column names
    # compute daily return for row 1 onwards
    normalizing_data = df/ df.iloc[0,:] # cumulative return is the same as normalitation
    #normalizing_data.iloc[0, :] = 0 # set daily returns  for  row 0 to 0
    return normalizing_data
     
    #return df/ df.ix[0,:]

def plot_data(df, title="Stock prices"):
    """Plot stock prices with a custom title and meaningful axis labels."""
    ax = df.plot(title=title, fontsize=12, figsize=(12,5))
    ax.set_xlabel("Date")
    ax.set_ylabel("Price")
    plt.show() # must be called to show pots in some enviroments

    
def test_run():
    # Define a date range
    dates = pd.date_range('2013-10-01', '2018-09-27')

    # Choose stock symbols to read
    #symbols = ['AAPL', 'IBM', 'AMZN']  # SPY will be added in get_data()
    symbols = ['AAPL', 'AMZN', 'GOOG', 'BKNG', 'IBM', 'MCD', 'MMM', 'NFLX', 'NVDA', 'PSX', 'TSLA', 'TXN']
    # Get stock data
    df = get_data(symbols, dates)

    # Slice and plot
    plot_data(df)

    # Compute daily returns
    normalizing_data = normalize_data(df)
    plot_data(normalizing_data, title="Normalized prices")
    
if __name__ == "__main__":
    test_run()

historical representation of price behavior by symbolo

In [13]:
# Getting data from Yahoo Finance
from pandas_datareader import data as pdr

import fix_yahoo_finance as yf
yf.pdr_override() # <== that's all it takes :-)

data = pdr.get_data_yahoo(['MMM', 'TXN', 'PSX', 'TSLA', 'IBM', 'AAPL', 'NVDA', 'BKNG', 'MCD', 'NFLX', 'AMZN', 'GOOG'], start="2013-10-01", end="2018-09-28")
[*********************100%***********************]  12 of 12 downloaded
In [14]:
data2 = data.drop(['Open', 'High', 'Low', 'Close', 'Volume'], axis = 1)
/home/felipemartinezs/anaconda3/envs/stock-price-indicator/lib/python3.6/site-packages/pandas/core/generic.py:3111: PerformanceWarning:

dropping on a non-lexsorted multi-index without a level parameter may impact performance.

In [15]:
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline

# Optional Plotly Method Imports
import plotly
import cufflinks as cf
cf.go_offline()
In [16]:
data2.xs(key='Adj Close', axis=1).plot(figsize=(20,8))
Out[16]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8e309035c0>
In [17]:
data2.xs(key='Adj Close', axis=1).iplot()

Statistics

In [19]:
tickers = ['MMM', 'TXN', 'PSX', 'TSLA', 'IBM', 'AAPL', 'NVDA', 'HD', 'MCD', 'NFLX', 'AMZN', 'GOOG']
In [20]:
for ticker in tickers:
    """Return the mean for stock indicated by symbol.
    
    Note: Data for a stock is stored in file: data//SP500/symbols/<symbol>.csv
    """
    df = pd.read_csv("data/SP500/symbols/{}.csv".format(ticker))  # read in data
    sta = df.describe()#(include='all')
    print()
    print("Describe Symbol: {}".format(ticker))
    print()
    
    print("%14s               %15s" % ('statistic', 'Indicators'))
    print(60 * "-")
    #print('-------------------------')
    print (sta)
Describe Symbol: MMM

     statistic                    Indicators
------------------------------------------------------------
              Open         High          Low        Close    Adj Close  \
count  1239.000000  1239.000000  1239.000000  1239.000000  1239.000000   
mean    172.480484   173.572486   171.406739   172.560315   162.968622   
std      31.337572    31.546574    31.041496    31.283243    34.954850   
min     117.269997   117.959999   116.650002   117.160004   103.509911   
25%     145.409996   146.514999   144.779999   145.540001   133.564445   
50%     166.300003   167.360001   165.279999   166.490005   154.599670   
75%     197.379997   198.735001   196.305001   197.560005   193.949737   
max     258.510010   259.769989   255.970001   258.630005   253.667068   

             Volume  
count  1.239000e+03  
mean   2.299762e+06  
std    1.026611e+06  
min    6.510000e+05  
25%    1.685700e+06  
50%    2.064400e+06  
75%    2.614850e+06  
max    1.167140e+07  

Describe Symbol: TXN

     statistic                    Indicators
------------------------------------------------------------
              Open         High          Low        Close    Adj Close  \
count  1239.000000  1239.000000  1239.000000  1239.000000  1239.000000   
mean     67.485246    68.071324    66.905262    67.525714    64.215650   
std      22.052627    22.264010    21.830864    22.064872    23.328729   
min      39.320000    39.490002    38.919998    39.240002    34.508915   
25%      49.150002    49.635000    48.774999    49.240000    45.218659   
50%      57.970001    58.509998    57.500000    58.160000    54.120201   
75%      80.670002    81.194999    80.099998    80.700001    78.218567   
max     120.019997   120.750000   118.900002   119.889999   117.864029   

             Volume  
count  1.239000e+03  
mean   5.610878e+06  
std    2.726565e+06  
min    1.288300e+06  
25%    3.917650e+06  
50%    4.972800e+06  
75%    6.532750e+06  
max    3.322960e+07  

Describe Symbol: PSX

     statistic                    Indicators
------------------------------------------------------------
              Open         High          Low        Close    Adj Close  \
count  1239.000000  1239.000000  1239.000000  1239.000000  1239.000000   
mean     83.945795    84.774181    83.130170    83.976513    78.282731   
std      12.013109    12.045087    11.982027    12.017346    14.045400   
min      56.820000    57.279999    56.500000    56.889999    49.378017   
25%      77.735000    78.545002    76.935001    77.814999    70.897435   
50%      80.610001    81.360001    79.879997    80.629997    74.578590   
75%      86.734997    87.465000    86.019997    86.715000    81.416229   
max     123.089996   123.970001   122.410004   123.339996   122.485252   

             Volume  
count  1.239000e+03  
mean   3.044059e+06  
std    1.434997e+06  
min    6.057000e+05  
25%    2.107100e+06  
50%    2.704900e+06  
75%    3.562650e+06  
max    1.266630e+07  

Describe Symbol: TSLA

     statistic                    Indicators
------------------------------------------------------------
              Open         High          Low        Close    Adj Close  \
count  1239.000000  1239.000000  1239.000000  1239.000000  1239.000000   
mean    249.680710   253.781227   245.334302   249.646029   249.646029   
std      57.849727    58.437837    57.117564    57.810804    57.810804   
min     119.379997   122.720001   116.099998   120.500000   120.500000   
25%     207.939995   211.150002   204.240005   207.510002   207.510002   
50%     236.699997   241.500000   233.250000   237.589996   237.589996   
75%     298.770004   304.705002   293.384995   299.610000   299.610000   
max     386.690002   389.609985   379.350006   385.000000   385.000000   

             Volume  
count  1.239000e+03  
mean   6.247020e+06  
std    3.913710e+06  
min    7.103000e+05  
25%    3.784450e+06  
50%    5.171500e+06  
75%    7.425650e+06  
max    3.268170e+07  

Describe Symbol: IBM

     statistic                    Indicators
------------------------------------------------------------
              Open         High          Low        Close    Adj Close  \
count  1239.000000  1239.000000  1239.000000  1239.000000  1239.000000   
mean    160.887667   161.988063   159.826763   160.909032   145.050353   
std      16.666731    16.721684    16.678932    16.704079    12.051852   
min     118.459999   119.660004   116.900002   117.849998   105.952515   
25%     147.970001   149.019996   147.019997   148.120002   138.205467   
50%     158.570007   159.660004   157.460007   158.490005   145.438721   
75%     173.795006   174.909996   172.800003   173.870003   153.656387   
max     198.050003   199.210007   195.880005   197.770004   171.052673   

             Volume  
count  1.239000e+03  
mean   4.418106e+06  
std    2.379891e+06  
min    1.193000e+06  
25%    3.115750e+06  
50%    3.888400e+06  
75%    4.915400e+06  
max    3.049020e+07  

Describe Symbol: AAPL

     statistic                    Indicators
------------------------------------------------------------
              Open         High          Low        Close    Adj Close  \
count  1239.000000  1239.000000  1239.000000  1239.000000  1239.000000   
mean    123.660532   124.679560   122.662985   123.708270   118.690082   
std      34.006643    34.234931    33.818106    34.045208    36.410131   
min      68.349998    69.228569    68.325714    68.705711    56.294216   
25%      98.685001    99.915001    98.010002    98.785000    93.720306   
50%     115.660004   116.510002   114.220001   115.309998   110.049706   
75%     147.754997   149.154999   146.799996   148.845002   145.753998   
max     223.250000   228.259995   222.399994   225.029999   225.029999   

             Volume  
count  1.239000e+03  
mean   4.492694e+07  
std    2.533896e+07  
min    1.147590e+07  
25%    2.670285e+07  
50%    3.818380e+07  
75%    5.617045e+07  
max    2.663808e+08  

Describe Symbol: NVDA

     statistic                    Indicators
------------------------------------------------------------
              Open         High          Low        Close    Adj Close  \
count  1239.000000  1239.000000  1239.000000  1239.000000  1239.000000   
mean     83.582316    84.713697    82.357224    83.625174    83.100458   
std      82.277587    83.406348    81.013551    82.286990    82.408911   
min      14.730000    14.870000    14.520000    14.550000    13.774642   
25%      19.925000    20.144999    19.660000    19.915000    19.330346   
50%      33.549999    33.919998    33.070000    33.750000    33.239483   
75%     145.830002   147.714996   142.900002   145.724998   145.236053   
max     277.230011   281.720001   276.320007   278.489990   278.489990   

             Volume  
count  1.239000e+03  
mean   1.092032e+07  
std    7.394229e+06  
min    1.141100e+06  
25%    6.400850e+06  
50%    8.926900e+06  
75%    1.274555e+07  
max    9.232320e+07  

Describe Symbol: HD

     statistic                    Indicators
------------------------------------------------------------
              Open         High          Low        Close    Adj Close  \
count  1259.000000  1259.000000  1259.000000  1259.000000  1259.000000   
mean    129.939238   130.893360   128.989945   129.969786   124.140546   
std      36.800286    37.049714    36.523823    36.798015    38.806954   
min      74.349998    74.639999    73.739998    74.139999    66.655449   
25%     102.915001   104.129998   101.724998   103.465000    95.496219   
50%     127.519997   128.820007   126.900002   127.739998   121.647652   
75%     153.435005   154.485001   152.660004   153.375000   149.215408   
max     214.029999   215.429993   211.500000   213.850006   213.850006   

             Volume  
count  1.259000e+03  
mean   5.264561e+06  
std    2.196903e+06  
min    1.517100e+06  
25%    3.819750e+06  
50%    4.725800e+06  
75%    6.095450e+06  
max    2.075320e+07  

Describe Symbol: MCD

     statistic                    Indicators
------------------------------------------------------------
              Open         High          Low        Close    Adj Close  \
count  1239.000000  1239.000000  1239.000000  1239.000000  1239.000000   
mean    121.486820   122.264132   120.763148   121.524617   113.975594   
std      26.626045    26.792622    26.414198    26.603132    29.820995   
min      88.070000    89.820000    87.500000    88.459999    79.360512   
25%      96.830002    97.435001    96.255001    96.915001    86.951541   
50%     116.529999   117.449997   115.790001   116.660004   109.223015   
75%     150.555000   151.615006   149.794998   151.184998   146.751671   
max     178.300003   178.699997   177.240005   178.360001   175.027328   

             Volume  
count  1.239000e+03  
mean   5.059390e+06  
std    2.564504e+06  
min    9.632000e+05  
25%    3.387100e+06  
50%    4.443700e+06  
75%    6.048750e+06  
max    2.528660e+07  

Describe Symbol: NFLX

     statistic                    Indicators
------------------------------------------------------------
              Open         High          Low        Close    Adj Close  \
count  1239.000000  1239.000000  1239.000000  1239.000000  1239.000000   
mean    130.717096   132.581588   128.742102   130.781400   130.781400   
std      87.664533    88.941018    86.255565    87.739052    87.739052   
min      42.889999    42.939999    40.400002    41.204285    41.204285   
25%      64.260716    64.887142    63.178570    64.121426    64.121426   
50%     100.480003   102.220001    98.820000   100.371429   100.371429   
75%     156.584999   158.250000   155.535004   157.205002   157.205002   
max     421.380005   423.209991   413.079987   418.970001   418.970001   

             Volume  
count  1.239000e+03  
mean   1.275878e+07  
std    1.128655e+07  
min    8.663000e+05  
25%    5.738700e+06  
50%    1.018550e+07  
75%    1.681470e+07  
max    1.810998e+08  

Describe Symbol: AMZN

     statistic                    Indicators
------------------------------------------------------------
              Open         High          Low        Close    Adj Close  \
count  1239.000000  1239.000000  1239.000000  1239.000000  1239.000000   
mean    738.193576   744.715916   730.614876   738.137288   738.137288   
std     420.424817   423.742378   416.368915   420.467939   420.467939   
min     284.399994   290.420013   284.000000   286.950012   286.950012   
25%     374.019989   378.150010   371.529999   374.259994   374.259994   
50%     658.650024   664.880005   652.000000   659.369995   659.369995   
75%     959.690002   966.519989   953.084992   959.515015   959.515015   
max    1997.420044  2025.569946  1986.900024  2002.380005  2002.380005   

             Volume  
count  1.239000e+03  
mean   3.982828e+06  
std    2.250929e+06  
min    1.093000e+06  
25%    2.647050e+06  
50%    3.420400e+06  
75%    4.507150e+06  
max    2.385610e+07  

Describe Symbol: GOOG

     statistic                    Indicators
------------------------------------------------------------
              Open         High          Low        Close    Adj Close  \
count  1239.000000  1239.000000  1239.000000  1239.000000  1239.000000   
mean    751.926212   757.882800   745.576307   751.976394   751.976394   
std     208.012075   209.974881   206.612914   208.458501   208.458501   
min     425.373352   428.537781   418.766327   424.076782   424.076782   
25%     560.820068   565.904816   555.654724   559.936340   559.936340   
50%     724.400024   730.000000   719.054993   725.270020   725.270020   
75%     925.304993   930.819489   918.165008   924.984985   924.984985   
max    1271.000000  1273.890015  1249.020020  1268.329956  1268.329956   

             Volume  
count  1.239000e+03  
mean   2.013415e+06  
std    1.333680e+06  
min    7.900000e+03  
25%    1.282050e+06  
50%    1.640200e+06  
75%    2.278650e+06  
max    2.328310e+07  

Bollinger bands

In [3]:
# Setting up a Bollinger Band with Python
# https://medium.com/python-data/setting-up-a-bollinger-band-with-python-28941e2fa300

# import needed libraries
import pandas as pd
import matplotlib.pyplot as plt
from pandas_datareader import data as web

# Make function for calls to Yahoo Finance
def get_adj_close(ticker, start, end):
    '''
    A function that takes ticker symbols, starting period, ending period
    as arguments and returns with a Pandas DataFrame of the Adjusted Close Prices
    for the tickers from Yahoo Finance
    '''
    start = start
    end = end
    info = web.DataReader(ticker, data_source='yahoo', start=start, end=end)['Adj Close']
    return pd.DataFrame(info)

# Get Adjusted Closing Prices for Facebook, Tesla and Amazon between 2016-2017
goog = get_adj_close('goog', '1/10/2018', '28/09/2018')
bkng = get_adj_close('bkng', '1/1/2018', '28/09/2018')
amazon = get_adj_close('amzn', '1/1/2018', '28/09/2018')

# Calculate 30 Day Moving Average, Std Deviation, Upper Band and Lower Band

for item in (goog, bkng, amazon):
    item['30 Day MA'] = item['Adj Close'].rolling(window=20).mean()
    item['30 Day STD'] = item['Adj Close'].rolling(window=20).std()
    item['Upper Band'] = item['30 Day MA'] + (item['30 Day STD'] * 2)
    item['Lower Band'] = item['30 Day MA'] - (item['30 Day STD'] * 2)


# Simple 30 Day Bollinger Band for Facebook (2016-2017)
goog[['Adj Close', '30 Day MA', 'Upper Band', 'Lower Band']].plot(figsize=(12,6))
plt.title('30 Day Bollinger Band for Google')
plt.ylabel('Price (USD)')
plt.show();
In [4]:
# set style, empty figure and axes
plt.style.use('fivethirtyeight')
fig = plt.figure(figsize=(12,6))
ax = fig.add_subplot(111)

# Get index values for the X axis for facebook DataFrame
x_axis = goog.index.get_level_values(0)

# Plot shaded 21 Day Bollinger Band for Facebook
ax.fill_between(x_axis, goog['Upper Band'], goog['Lower Band'], color='grey')

# Plot Adjust Closing Price and Moving Averages
ax.plot(x_axis, goog['Adj Close'], color='blue', lw=2)
ax.plot(x_axis, goog['30 Day MA'], color='black', lw=2)

# Set Title & Show the Image
ax.set_title('30 Day Bollinger Band For Google')
ax.set_xlabel('Date (Year/Month)')
ax.set_ylabel('Price(USD)')
ax.legend()
plt.show();

Return

In [1]:
"""Compute daily returns."""

import os
import pandas as pd
import matplotlib.pyplot as plt

def symbol_to_path(symbol, base_dir="data/SP500/symbols"):
    """Return CSV file path given ticker symbol."""
    return os.path.join(base_dir, "{}.csv".format(str(symbol)))

def get_data(symbols, dates):
    """Read stock data (adjusted close) for given symbols from CSV files."""
    df = pd.DataFrame(index=dates)
    if 'AAPL' not in symbols:  # add SPY for reference, if absent
        symbols.insert(0, 'AAPL')

    for symbol in symbols:
        df_temp = pd.read_csv(symbol_to_path(symbol), index_col='Date',
                parse_dates=True, usecols=['Date', 'Adj Close'], na_values=['nan'])
        df_temp = df_temp.rename(columns={'Adj Close': symbol})
        df = df.join(df_temp)
        if symbol == 'AAPL':  # drop dates SPY did not trade
            df = df.dropna(subset=["AAPL"])

    return df

def plot_data(df, title="Stock prices", xlabel="Date", ylabel="Price"):
    """Plot stock prices with a custom title and meaningful axis labels."""
    ax = df.plot(title=title, fontsize=12, figsize=(9,4))
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    plt.show()


def compute_daily_returns(df):
    """Compute and return the daily return values."""
    # TODO: Your code here
    # Note: Returned DataFrame must have the same number of rows
    daily_returns = df.copy() # copy gibe DtaFrame to match size and column names
    # compute daily return for row 1 onwards
    daily_returns[1:] = (df[1:] / df[:-1].values) - 1
    #daily_returns.ix[0, :] = 0 # set daily returns  for  row 0 to 0
    daily_returns.iloc[0, :] = 0 # set daily returns  for  row 0 to 0
    return daily_returns

def test_run():
    # Read data
    dates = pd.date_range('2013-10-03', '2018-09-21')  # one month only
    #symbols =  ['AAPL', 'AMZN', 'GOOG', 'HD', 'IBM', 'MCD', 'MMM', 'NFLX', 'NVDA', 'PSX', 'TSLA', 'TXN']
    symbols = ['AAPL']#, 'GOOG', 'HD', 'IBM', 'MCD', 'MMM', 'NFLX', 'NVDA', 'PSX', 'TSLA', 'TXN']
    df = get_data(symbols, dates)
    plot_data(df)

    # Compute daily returns
    daily_returns = compute_daily_returns(df)
    plot_data(daily_returns, title="Daily returns", ylabel="Daily returns")


if __name__ == "__main__":
    test_run()

Cumulative return

In [7]:
"""Compute cumulative returns."""

import os
import pandas as pd
import matplotlib.pyplot as plt

def symbol_to_path(symbol, base_dir="data/SP500/symbols"):
    """Return CSV file path given ticker symbol."""
    return os.path.join(base_dir, "{}.csv".format(str(symbol)))

def get_data(symbols, dates):
    """Read stock data (adjusted close) for given symbols from CSV files."""
    df = pd.DataFrame(index=dates)
    if 'AMZN' not in symbols:  # add SPY for reference, if absent
        symbols.insert(0, 'AMZN')

    for symbol in symbols:
        df_temp = pd.read_csv(symbol_to_path(symbol), index_col='Date',
                parse_dates=True, usecols=['Date', 'Adj Close'], na_values=['nan'])
        df_temp = df_temp.rename(columns={'Adj Close': symbol})
        df = df.join(df_temp)
        if symbol == 'AMZN':  # drop dates SPY did not trade
            df = df.dropna(subset=["AMZN"])

    return df

def plot_data(df, title="Stock prices", xlabel="Date", ylabel="Price"):
    """Plot stock prices with a custom title and meaningful axis labels."""
    ax = df.plot(title=title, fontsize=12, figsize=(7, 3))
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    plt.show()


def compute_cumulative_returns(df):
    """Compute and return the daily return values."""
    # TODO: Your code here
    # Note: Returned DataFrame must have the same number of rows
    
    cumulative_returns = (df / df.iloc[0]) - 1
        
    return cumulative_returns
    
    #daily_returns = df.copy() # copy gibe DtaFrame to match size and column names
    # compute daily return for row 1 onwards
    #cumula_returns = (df / df.iloc[0]) - 1 # cumulative return is the same as normalitation
    #daily_returns.iloc[0, :] = 0 # set daily returns  for  row 0 to 0
    #return daily_returns

def test_run():
    # Read data
    dates = pd.date_range('2018-08-01', '2018-09-28')  # one month only
    #symbols =  ['AAPL', 'AMZN', 'GOOG', 'HD', 'IBM', 'MCD', 'MMM', 'NFLX', 'NVDA', 'PSX', 'TSLA', 'TXN']
    symbols = ['AMZN']#, 'GOOG', 'HD', 'IBM', 'MCD', 'MMM', 'NFLX', 'NVDA', 'PSX', 'TSLA', 'TXN']
    df = get_data(symbols, dates)
    plot_data(df)

    # Compute daily returns
    cumulative_returns = compute_cumulative_returns(df)
    plot_data(cumulative_returns, title="Cumulative returns", ylabel="Cumulative returns")


if __name__ == "__main__":
    test_run()
In [8]: